package com.huishu.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.Result;
import com.huishu.coupon.mapper.CouponProductRelationMapper;
import com.huishu.coupon.pojo.model.CouponProductRelationDO;
import com.huishu.coupon.service.CouponProductRelationService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-10
 */
@Service
public class CouponProductRelationServiceImpl extends ServiceImpl<CouponProductRelationMapper, CouponProductRelationDO> implements CouponProductRelationService {

    @Override
    public Result bindingCoupon(Long id, Long couponId) {
        LambdaQueryWrapper<CouponProductRelationDO> queryWrapper = Wrappers.lambdaQuery(CouponProductRelationDO.class)
                .eq(CouponProductRelationDO::getCouponId, couponId)
                .eq(CouponProductRelationDO::getProductId, id);
        CouponProductRelationDO one = this.getOne(queryWrapper);
        if (one != null) {
            return Result.fail("已经绑定该优惠卷了！！");
        }

        CouponProductRelationDO couponProductRelationDO = new CouponProductRelationDO();
        couponProductRelationDO.setCouponId(couponId);
        couponProductRelationDO.setProductId(id);
        boolean b = this.save(couponProductRelationDO);
        return b ? Result.success() : Result.fail();
    }

    @Override
    public Result deleteCoupon(Long id, Long couponId) {
        LambdaQueryWrapper<CouponProductRelationDO> queryWrapper = Wrappers.lambdaQuery(CouponProductRelationDO.class)
                .eq(CouponProductRelationDO::getCouponId, couponId)
                .eq(CouponProductRelationDO::getProductId, id);
        boolean remove = this.remove(queryWrapper);
        return remove ? Result.success() : Result.fail();
    }
}
